package com.ybt.router.core.receiver.dispatch.impl;

import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;

import com.ybt.router.core.message.IMSDMessage;
import com.ybt.router.core.message.IMSDMessageConstants;
import com.ybt.router.core.receiver.dispatch.IMessageDispatch;
import com.ybt.router.core.receiver.exception.MessageReceiverException;
import com.ybt.router.core.receiver.processor.IMessageProcessor;
import com.ybt.router.core.receiver.processor.factory.MessageProcessorFactory;

public class ByteMessageDispatch implements IMessageDispatch<byte[]> {
    private static Logger logger = Logger.getLogger(ByteMessageDispatch.class);
    private List<IMSDMessage<byte[]>> receivedMessages = new LinkedList<IMSDMessage<byte[]>>();
    
    public ByteMessageDispatch(){
        new Thread(){
            List<IMSDMessage<byte[]>> dispMessages = new LinkedList<IMSDMessage<byte[]>>();
            @Override
            public void run() {
                // TODO Auto-generated method stub
                while(true){
                    synchronized(receivedMessages){
                        if(receivedMessages.size()==0){
                            //没有消息，则等待
                            try {
                                receivedMessages.wait();
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                logger.error("the dispatcher thead is interrupted", e);
                            }
                        }else{
                            //存在消息，则分发
                            //先将分发消息，移除到临时队列
                            while(receivedMessages.size()>0){
                                dispMessages.add(receivedMessages.remove(0));
                            }
                        }
                        //释放锁
                    }
                    //开始分发消息
                    while(dispMessages.size()>0){
                        IMSDMessage<byte[]> msg = dispMessages.remove(0);
                        String msgType = msg.getProperty(IMSDMessageConstants.MSD_MESSAGE_TYPE);
                        //根据消息类型获取消息处理器
                        IMessageProcessor processor = MessageProcessorFactory.getInstance().createMessageProcessor(msgType);
                        try {
                            //处理消息
                            processor.process(msg);
                        } catch (MessageReceiverException e) {
                            //FIXME 暂时不考虑资源释放问题，现实中不应该出现异常
                            logger.error("process the message error", e);
                        }
                    }
                    
                }
            }
            
        }.start();
    }
    public void onMessage(IMSDMessage<byte[]> message) throws MessageReceiverException {
        // TODO Auto-generated method stub
        //将消息放入缓冲队列
        synchronized(receivedMessages){
            receivedMessages.add(message);
            receivedMessages.notify();
        }
        
        
    }
    

}
